// pages/empower/empower.js
// 授权页
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    logoImg: '/assets/imgs/logo.png',
    userInfo: {},
    hasUserInfo: false,
    rawData: '',
    signature: '',
    encryptedData: '',
    iv: '',
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    sessionKey: ''
  },
  getUserInfo: function(e) {
    let that = this
    let config = wx.getStorageSync('config')
    // 马上授权
    that.setData({
      encryptedData: e.detail.encryptedData,
      iv: e.detail.iv,
      rawData: e.detail.rawData,
      signature: e.detail.signature
    })
    // getUserInfo
    if (e.detail.errMsg == "getUserInfo:ok") {
      app.globalData.userInfo = e.detail.userInfo
      that.setData({
        userInfo: e.detail.userInfo,
        hasUserInfo: true,
        rawData: e.detail.rawData,
        signature: e.detail.signature,
        encryptedData: e.detail.encryptedData,
        iv: e.detail.iv
      })
      wx.redirectTo({
        url: '/pages/signIn/signIn',
      })
    } else {
      wx.showToast({
        title: '请允许授权',
        icon: 'none'
      })
    }
    // getCode
    // wx.login({
    //   success(res) {
    //     if (res.code) {
    //       // 发送用户所有需要信息给后台，并跳转登录页面
    //       wx.request({
    //         url: config.host+'api/wx/login',
    //         method: 'POST',
    //         data: res.code,
    //         success(res) {
    //           let sessionKey = res.data.data.sessionKey
    //           that.setData({
    //             sessionKey: sessionKey
    //           })
    //           wx.request({
    //             url: config.host+'api/wx/phone',
    //             method: 'POST',
    //             data: {
    //               "encryptedData": that.data.encryptedData,
    //               "iv": that.data.iv,
    //               "rawData": that.data.rawData,
    //               "sessionKey": that.data.sessionKey,
    //               "signature": that.data.signature
    //             },
    //             success(res) {
    //               console.log(res)
    //             }
    //           })
    //         }
    //       })
    //     } else {
    //       console.log('登录失败' + res.errMsg);
    //     }
    //   }
    // })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
      wx.reLaunch({
        url: '/pages/signIn/signIn',
      })
    } else if (this.data.canIUse) {
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
        wx.reLaunch({
          url: '/pages/signIn/signIn',
        })
      }
    } else {
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
          wx.reLaunch({
            url: '/pages/signIn/signIn',
          })
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  }
})